home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 11472 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.3 KB  |  88 lines

  1. Path: lonestar.jpl.utsa.edu!nreitzel
  2. From: nreitzel@lonestar.jpl.utsa.edu (Norman L. Reitzel   )
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Different "new" in g++ and TC++
  5. Date: 14 Mar 1996 19:07:26 GMT
  6. Organization: University of Texas at San Antonio
  7. Message-ID: <4i9qpe$48c@ringer.cs.utsa.edu>
  8. References: <3148391A.4646@numerik.uni-kiel.de> <4i9mjg$2m1@cloner3.netcom.com>
  9. NNTP-Posting-Host: lonestar.jpl.utsa.edu
  10.  
  11. In article <4i9mjg$2m1@cloner3.netcom.com> jlilley@ix.netcom.com (John Lilley) writes:
  12.  
  13. >>A is a class with constructor A::A() and n is an
  14. >>integer, apparently g++ and TC++ 3.0 do different jobs
  15. >>on the expression "new A[n]": TC++ calls the constructor
  16. >>apparently for every created object, while g++ does not.
  17. >
  18. >TC++ is correct.  See ARM section 5.3.3, page 61:
  19. >
  20. >    "Arrays of objects... if the class has a default constructor. (12.1)
  21. >    In that case, the default constructor will be called for each
  22. >    element of the array"
  23.  
  24. The assertion that TC++ and g++ handle this case differently is untrue.
  25.  
  26. This is the program that was run as a test:
  27.  
  28.     #include <stdio.h>
  29.  
  30.     class WC {
  31.         private:
  32.             int goof;
  33.         public:
  34.             WC();
  35.             ~WC();
  36.     };
  37.  
  38.     int main( void ) {
  39.         WC *wcp = new WC[4];
  40.         printf("Allocated!\n");
  41.         delete [] wcp;
  42.         printf("Deleted!\n");
  43.         return 0;
  44.     }
  45.  
  46.     WC::WC() {
  47.         goof = 0;
  48.         printf("Default Constructor Called.\n");
  49.     }
  50.  
  51.     WC::~WC() {
  52.         printf("Default Destructor Called.\n");
  53.     }
  54.  
  55. Note that both the constructor and destructor are default, and
  56. that neither of them is inline.  I didn't test other cases,
  57. but have no a priori reason to suspect that they will behave
  58. differently.  Here is the result of running this test using
  59. emx09b running on OS/2 3.0 Fixpack 17 with bash 1.1.12:
  60.  
  61.     $ gcc --version
  62.       2.7.0
  63.     $ gcc -static -o testit.exe testit.cc
  64.     $ testit
  65.  
  66.     Default Constructor Called.
  67.     Default Constructor Called.
  68.     Default Constructor Called.
  69.     Default Constructor Called.
  70.     Allocated!
  71.  
  72.     Default Destructor Called.
  73.     Default Destructor Called.
  74.     Default Destructor Called.
  75.     Default Destructor Called.
  76.     Deleted!
  77.  
  78. As you can see, the default constructor is called for each
  79. of the elements of the array, as is the destructor.
  80.  
  81.  
  82.  
  83.  
  84. -- 
  85.    Norman L. Reitzel, Jr.       |    "When you live beside the graveyard,
  86.    nreitzel@lonestar.utsa.edu   |     you can't cry for every funeral."
  87.    Blue Water Ventures, dba.    |                     Russian Proverb
  88.